package com.app.micaihu.view.user.userinfo.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.AttentionAndFansBean;
import com.app.micaihu.bean.infor.AttentionStautsBean;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.utils.ImageLoadUtils;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guo on 2016/8/26.
 * 粉丝和关注列表的适配器
 * <p/>
 * status :0-未关注; 1-已关注; 2-互相关注
 */
public class AttentionAndFansAdapter extends AppBaseAdapter<AttentionAndFansBean> implements View.OnClickListener {

    private String type;//是关注(1)    还是粉丝(2)
    private boolean other;

    public AttentionAndFansAdapter(List<AttentionAndFansBean> list, Context context, String type, boolean other) {
        super(list, context);
        this.type = type;
        this.other = other;
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_myhome_attentionandfans, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AttentionAndFansBean attentionAndFansBean = list.get(position);
        if (attentionAndFansBean != null) {
//            LoadImageUtils.getInstance().loadCircleImage(viewHolder.civHead, attentionAndFansBean.getHeadPic());
            LoadImageUtils.getInstance().loadListCircleImage(viewHolder.civHead, attentionAndFansBean.getHeadPic());

            viewHolder.tvNickName.setText(attentionAndFansBean.getNickName());

            if (com.blankj.utilcode.util.StringUtils.isEmpty(attentionAndFansBean.getHonourPic())) {
                viewHolder.ivHonourPic.setVisibility(View.GONE);
            } else {
                viewHolder.ivHonourPic.setVisibility(View.VISIBLE);
                ImageLoadUtils.loadImage(attentionAndFansBean.getHonourPic(), viewHolder.ivHonourPic);
            }
            ImageLoadUtils.loadImage(attentionAndFansBean.getRankIcon(), viewHolder.ivRankIcon);
            viewHolder.tvRankName.setText(attentionAndFansBean.getRankName());
            viewHolder.tvArmyGroupInfo.setText(attentionAndFansBean.getArmyGroupInfo());
//            if(attentionAndFansBean.getIsAuthor()){
//                viewHolder.imProdateHead.setVisibility(View.VISIBLE);
//            }else{
//                viewHolder.imProdateHead.setVisibility(View.GONE);
//            }
            if(TextUtils.isEmpty(attentionAndFansBean.getSignature())){
                viewHolder.tvSignature.setText(AppApplication.getInstance().getString(R.string.user_signature));
            }else{
                viewHolder.tvSignature.setText(attentionAndFansBean.getSignature());
            }

            String uid = attentionAndFansBean.getUid();
            if(TextUtils.equals(type,"1") && !other){
                viewHolder.tvAttention.setVisibility(View.GONE);
            }else{

                if (UserInforManager.getInstance().isLogin()) {
                    if (TextUtils.equals(UserInforManager.getInstance().getUserInfor().getUid(), uid)) {
                        viewHolder.tvAttention.setVisibility(View.GONE);
                    } else {
                        showAttentionBtn(viewHolder, attentionAndFansBean);
                    }
                } else {
                    showAttentionBtn(viewHolder, attentionAndFansBean);
                }
            }

        }


        return convertView;
    }



    private void showAttentionBtn(ViewHolder viewHolder, AttentionAndFansBean attentionAndFansBean) {
        viewHolder.tvAttention.setVisibility(View.VISIBLE);
        String status = attentionAndFansBean.getStatus();
        //0 未关注 1 已关注 2 互相关注,
        if (TextUtils.equals(status, "0")) {//未关注:我的粉丝->显示绿色边框,绿色+号
            viewHolder.tvAttention.getCompoundDrawables()[0].setLevel(4);
            viewHolder.tvAttention.setSelected(true);
            viewHolder.tvAttention.setText(context.getResources().getString(R.string.homepage_attention));
        } else if (TextUtils.equals(status, "1")) {//已关注:我的关注:显示灰色边框,灰色对勾
            viewHolder.tvAttention.getCompoundDrawables()[0].setLevel(5);
            viewHolder.tvAttention.setSelected(false);
            viewHolder.tvAttention.setText(context.getResources().getString(R.string.homepage_attentioned));
        } else {//互相关注  灰色边框 灰色字体 灰色互相
            viewHolder.tvAttention.getCompoundDrawables()[0].setLevel(1);
            viewHolder.tvAttention.setSelected(false);
            viewHolder.tvAttention.setText(context.getResources().getString(R.string.homepage_attention_eachother));
        }
        viewHolder.tvAttention.setTag(attentionAndFansBean);
    }

    @Override
    public void onClick(View v) {
        if (v == null) return;
        if (v.getId() == R.id.tv_attention) {
            attentionClick(v);
        }
    }


    class ViewHolder {
        CustomImageView civHead;
        TextView tvNickName;
        TextView tvAttention;
        TextView tvSignature;
        TextView tvRankName;
        TextView tvArmyGroupInfo;
        ImageView ivHonourPic;
        ImageView ivRankIcon;

        ViewHolder(View convertView) {
            civHead = (CustomImageView) convertView.findViewById(R.id.civ_head);
            tvNickName = (TextView) convertView.findViewById(R.id.tv_nickname);
            tvAttention = (TextView) convertView.findViewById(R.id.tv_attention);
            tvSignature = (TextView) convertView.findViewById(R.id.tv_signature);
            ivRankIcon = convertView.findViewById(R.id.ivRankIcon);
            tvRankName = convertView.findViewById(R.id.tvRankName);
            tvArmyGroupInfo = convertView.findViewById(R.id.tvArmyGroupInfo);
            ivHonourPic = convertView.findViewById(R.id.ivHonourPic);

            tvAttention.setOnClickListener(AttentionAndFansAdapter.this);
        }
    }


    /**
     * 关注/取消关注
     *
     * @param v
     */
    public void attentionClick(View v) {
        if (v == null) return;
        if (UserInforManager.getInstance().isLogin()) {
            Object tag = v.getTag();
            if (tag instanceof AttentionAndFansBean) {
                final AttentionAndFansBean attentionAndFansBean = (AttentionAndFansBean) tag;
                Map<String, String> params = new HashMap<>();
                if (UserInforManager.getInstance().isLogin()) {
                    params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
                } else {
                    params.put("uid", "0");
                }

                params.put("toUid", attentionAndFansBean.getUid());
                String currentRequestType = "";
                if (TextUtils.equals(attentionAndFansBean.getStatus(), "0")) {     //未关注->点击去关注
                    currentRequestType = "1";
                } else {//取消关注
                    currentRequestType = "2";
                }
                params.put("type", currentRequestType);

                HttpListener<DataBean<AttentionStautsBean>> attentionListener = new HttpListener<DataBean<AttentionStautsBean>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        if (context != null) {
                            DialogUtils.getInstance().showProgressDialog((Activity) context, AppApplication.getInstance().getResources().getString(R.string.homepage_attention_start));
                        }

                    }

                    @Override
                    public void onError(VolleyError error) {
                        DialogUtils.getInstance().dismissProgressDialog();
                        if (context != null) {
                            ToastUtils.makeText(context.getResources().getString(R.string.neterror));
                        }
                    }

                    @Override
                    public void onSuccess(DataBean<AttentionStautsBean> response) {
                        DialogUtils.getInstance().dismissProgressDialog();
                        if (response.noError() && response.getData() != null) {
                            AttentionStautsBean attentionStautsBean = response.getData();
                            if (attentionStautsBean != null) {
                                if (TextUtils.equals(attentionAndFansBean.getStatus(), "0")) {     //未关注->点击去关注
                                    ToastUtils.makeText(AppApplication.getInstance().getResources().getString(R.string.homepage_attentionsucc));
                                } else {//取消关注
                                    ToastUtils.makeText(AppApplication.getInstance().getResources().getString(R.string.homepage_attentioncanclesucc));
                                }

                                attentionAndFansBean.setStatus(attentionStautsBean.getStatus());
                                notifyDataSetChanged();

                            } else {
                                ToastUtils.makeText(AppApplication.getInstance().getResources().getString(R.string.dataerror));
                            }
                        } else {
                            ToastUtils.makeText(response.getNnderstoodMsg());


                        }
                    }
                };

                HttpUtils.addPostRequest(URIContant.ATTENTION_OR_NO, new TypeToken<DataBean<AttentionStautsBean>>() {
                }.getType(), "", params, attentionListener);
            }
        } else {
            if (context != null) {
                UserInforManager.getInstance().toLoginPage((Activity) context);
            }
        }

    }


}
